package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.com.kunpeng.entity.*;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.ProjectPurchaseQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.UpdateOaProjectPurchaseDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.*;
import org.jeecg.modules.demo.com.kunpeng.service.ICommodityPriceService;
import org.jeecg.modules.demo.com.kunpeng.service.IProjectPurchaseService;
import org.jeecg.modules.demo.com.kunpeng.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.rowset.serial.SerialException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.rmi.MarshalException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目采购表
 * @Author: jeecg-boot
 * @Date:   2022-12-15
 * @Version: V1.0
 */
@Service
@Slf4j
public class ProjectPurchaseServiceImpl extends ServiceImpl<ProjectPurchaseMapper, ProjectPurchase> implements IProjectPurchaseService {

	private static Integer PROJECT_PURCHASE_SUFFIX;
	@Autowired
	private ProjectPurchaseMapper projectPurchaseMapper;
	@Autowired
	private ProjectPurchaseApplyMapper projectPurchaseApplyMapper;
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private ICommodityPriceService commodityPriceService;
	@Autowired
	private OaApproveMapper oaApproveMapper;
	@Autowired
	private OaFlowRecordMapper oaFlowRecordMapper;
	@Autowired
	private OaFlowStepMapper oaFlowStepMapper;
	@Autowired
	private OaMessageMapper oaMessageMapper;
	@Autowired
	private OaAdminMapper oaAdminMapper;
	@Autowired
	private OaProjectMapper oaProjectMapper;
	@Autowired
	private OaStepMapper oaStepMapper;
	@Autowired
	private OaAdminLogMapper oaAdminLogMapper;

	/**
	 * 主副表插入
	 * @param projectPurchase
	 * @param projectPurchaseApplyList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ProjectPurchase projectPurchase, List<ProjectPurchaseApply> projectPurchaseApplyList) {
		ProjectStandardVO projectStandardVO = projectMapper.selectByProjectId(projectPurchase.getPjId());
		if(null!=projectStandardVO){
			projectPurchase.setProjectStage(projectStandardVO.getCurrentSpeed());
		}
		BigDecimal purchaseMoney=new BigDecimal(0.00);
		/*log.info("projectPurchaseApplyList:"+projectPurchaseApplyList.toArray().toString());*/
		for(ProjectPurchaseApply projectPurchaseApply:projectPurchaseApplyList){
			BigDecimal num = BigDecimal.valueOf(projectPurchaseApply.getRequestedQuantity());
			CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceService.selectCommodityByCommodityCode(projectPurchaseApply.getCommodityCode());
			if(null!=commodityPriceQueryListVO) {
				purchaseMoney = commodityPriceQueryListVO.getPurchaseIncludTax().multiply(num);
			}
		}
		/*LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();*/
		/*projectPurchase.setApplyPerson(sysUser.getUsername());*/
		projectPurchase.setPurchaseMoney(purchaseMoney);
		List<ProjectPurchase> projectPurchases = projectPurchaseMapper.listByPurchaseId(projectPurchase.getPurchaseId());
		System.out.println("***************projectPurchases："+projectPurchase.toString());
		if(projectPurchases.size()>0){
			String message="采购单号已存在，请重新填写！";
			log.info("采购单号已存在，请重新填写！");
			throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
		}
		projectPurchase.setId(null);
		projectPurchaseMapper.insert(projectPurchase);
		log.info("项目采购表插入成功！");
		//插入子表数据
		if(null!=projectPurchaseApplyList){
			for (ProjectPurchaseApply p:projectPurchaseApplyList) {
				System.out.println("准备插入采购申请表子表");
				p.setId(null);
				p.setPpId(projectPurchase.getId());
				projectPurchaseApplyMapper.insert(p);
				log.info("项目采购申请表插入成功！");
			}
		}
	}

	/**
	 * 主副表编辑
	 * @param projectPurchase
	 * @param projectPurchaseApplyList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ProjectPurchase projectPurchase,List<ProjectPurchaseApply> projectPurchaseApplyList) {
		ProjectStandardVO projectStandardVO = projectMapper.selectByProjectId(projectPurchase.getPjId());
		projectPurchase.setProjectStage(projectStandardVO.getCurrentSpeed());
		BigDecimal purchaseMoney=null;
		/*System.out.println("编辑：projectPurchaseApplyList:"+projectPurchaseApplyList.toString());*/
		for(ProjectPurchaseApply p:projectPurchaseApplyList){
			BigDecimal num = BigDecimal.valueOf(p.getRequestedQuantity());
			CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceService.selectCommodityByCommodityCode(p.getCommodityCode());
			if(null!=commodityPriceQueryListVO) {
				purchaseMoney = commodityPriceQueryListVO.getPurchaseIncludTax().multiply(num);
			}
		}
		projectPurchase.setPurchaseMoney(purchaseMoney);
		projectPurchaseMapper.updateById(projectPurchase);
		//修改子表数据
		for (ProjectPurchaseApply p:projectPurchaseApplyList) {
			projectPurchaseApplyMapper.deleteByPPId(p.getPpId());
		}
		for (ProjectPurchaseApply p:projectPurchaseApplyList) {
			p.setId(null);
			p.setPpId(projectPurchase.getId());
			projectPurchaseApplyMapper.insert(p);
			log.info("项目采购申请表修改成功！");
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		projectPurchaseMapper.deleteById(id);
		projectPurchaseApplyMapper.deleteByPPId(id);
	}

	/**
	 * 批量删除
	 * @param idList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			projectPurchaseMapper.deleteById(id);
			projectPurchaseApplyMapper.deleteByPPId(id.toString());
		}
	}

	/**
	 * 分页全局模糊查询
	 * @param page
	 * @param projectPurchaseQueryDTO
	 * @return
	 */
	@Override
	public Page<ProjectPurchaseListQueryVO> listByKeyWords(Page<ProjectPurchaseListQueryVO> page, ProjectPurchaseQueryDTO projectPurchaseQueryDTO) {
		log.info("开始处理模糊查询请求：service");

		Page<ProjectPurchaseListQueryVO> list = page.setRecords(projectPurchaseMapper.listByKeyWords(page, projectPurchaseQueryDTO.getStartTime(), projectPurchaseQueryDTO.getEndTime(),
				projectPurchaseQueryDTO.getProjectName(), projectPurchaseQueryDTO.getResponsibilityPerson(),
				projectPurchaseQueryDTO.getKeyWords()));

		if(list.getRecords().size()>0){
			for(ProjectPurchaseListQueryVO projectPurchaseListQueryVO:list.getRecords()){
				OaApprove oaApprove = oaApproveMapper.selectOaApproveByPpId(projectPurchaseListQueryVO.getId());
				if(null!=oaApprove){
					projectPurchaseListQueryVO.setStatus(oaApprove.getCheckStatus());
					projectPurchaseMapper.updateProjectPurchaseStatus(projectPurchaseListQueryVO.getId(),oaApprove.getCheckStatus().toString());
				}
			}
		}
		/*for (ProjectPurchaseListQueryVO p:list.getRecords()) {
			System.out.println("***************查询到的数据："+p.toString());
			ProjectStandardVO projectStandardVO = projectMapper.selectByProjectId(p.getPjId());
			p.setResponsibilityPerson(projectStandardVO.getResponsibilityPerson());
		}*/
		return list;
	}

	/**
	 * 根据id查询项目采购详情
	 * @param id
	 * @return
	 */
	@Override
	public ProjectPurchaseDetailsVO selectProjectPurchaseDetailsById(String id) {
		log.info("根据项目采购表id获取项目采购详情：service");
		ProjectPurchaseDetailsVO projectPurchaseDetailsVO = projectPurchaseMapper.selectProjectPurchaseDetailsById(id);
		if(projectPurchaseDetailsVO==null){
			String message="没有符合条件的数据！";
			log.info(message);
			throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
		}
		List<ProjectPurchaseApply> projectPurchaseApplies = projectPurchaseApplyMapper.listByPPId(id);
		projectPurchaseDetailsVO.setProjectPurchaseApplyList(projectPurchaseApplies);
		System.out.println("项目采购详情："+projectPurchaseDetailsVO);
		return projectPurchaseDetailsVO;
	}


	/**
	 * 根据项目采购id查询项目采购申请
	 * @param PPId
	 * @return
	 */
	@Override
	public List<ProjectPurchaseApply> listByPPId(String PPId) {
		List<ProjectPurchaseApply> projectPurchaseApplies = projectPurchaseApplyMapper.listByPPId(PPId);
		return projectPurchaseApplies;
	}

	//根据项目编号查询项目采购列表
	@Override
	public Page<ProjectPurchaseListQueryVO> listByProjectId(Page<ProjectPurchaseListQueryVO> page, String projectId) {
		Page<ProjectPurchaseListQueryVO> projectPurchaseListQueryVOPage = page.setRecords(projectPurchaseMapper.listByProjectId(page, projectId));
		return projectPurchaseListQueryVOPage;
	}

	/**
	 * 根据采购单号前缀模糊查询采购列表
	 */
	@Override
	public Integer listPurchaseIdByPurchaseId(String purchasePrefix) {
		List<ProjectPurchase> projectPurchases = projectPurchaseMapper.listPurchaseIdByPurchaseId(purchasePrefix);
		/*System.out.println("采购单号前缀："+purchasePrefix);*/
		/*System.out.println("根据采购前缀获取到的项目采购数据："+projectPurchases.toString());*/
		if(projectPurchases.size()>0){
			ProjectPurchase projectPurchase = projectPurchases.get(projectPurchases.size() - 1);
			String purchaseId = projectPurchase.getPurchaseId();
			String substring = purchaseId.substring(12, 15);
			/*System.out.println("substring："+substring);*/
			PROJECT_PURCHASE_SUFFIX = Integer.parseInt(substring)+1;
		}else {
			PROJECT_PURCHASE_SUFFIX=1;
		}
		return PROJECT_PURCHASE_SUFFIX;
	}

	/**
	 * 新增项目采购审批
	 * @param oaProjectPurchasePage
	 */
	@Override
	@Transactional
	public void insertOaProjectPurchase(OaProjectPurchasePage oaProjectPurchasePage) {
		//项目采购详情
		List<ProjectPurchaseApply> projectPurchaseApplyList = oaProjectPurchasePage.getProjectPurchaseApplyList();
		ProjectPurchase projectPurchase=new ProjectPurchase();
		BeanUtils.copyProperties(oaProjectPurchasePage,projectPurchase);

		//根据项目名称获取项目详情
		OaProject oaProjectDetails = oaProjectMapper.selectOaProjectByName(oaProjectPurchasePage.getProjectName());
		if(null!=oaProjectDetails && null!=oaProjectDetails.getProjectId()) {
			projectPurchase.setPjId(oaProjectDetails.getProjectId());
		}

		//根据id获取项目阶段详情
		List<OaStep> oaSteps = oaStepMapper.listOaStep(oaProjectDetails.getId());
		for (OaStep oaStep:oaSteps) {
			Long createTime = System.currentTimeMillis();
			Integer currentTime = createTime.intValue();
			Integer startTime = oaStep.getStartTime();
			Integer endTime = oaStep.getEndTime();
			if(currentTime>startTime&&currentTime<endTime){
				//TODO 项目阶段赋值已修改
				projectPurchase.setProjectStage(oaStep.getFlowName());
			}

		}
		/*System.out.println("*****项目采购大集合："+oaProjectPurchasePage.toString());*/
		BigDecimal purchaseMoney=new BigDecimal(0.00);
		/*log.info("projectPurchaseApplyList:"+projectPurchaseApplyList.toArray().toString());*/
		for(ProjectPurchaseApply projectPurchaseApply:projectPurchaseApplyList){
			/*System.out.println("商品采购数量："+projectPurchaseApply.getRequestedQuantity());*/
			BigDecimal num = BigDecimal.valueOf(projectPurchaseApply.getRequestedQuantity());
			CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceService.selectCommodityByCommodityCode(projectPurchaseApply.getCommodityCode());
			/*System.out.println("采购商品信息："+commodityPriceQueryListVO.toString());*/
			if(null!=commodityPriceQueryListVO) {
				purchaseMoney = commodityPriceQueryListVO.getPurchaseIncludTax().multiply(num);
			}
		}
		projectPurchase.setPurchaseMoney(purchaseMoney);
		List<ProjectPurchase> projectPurchases = projectPurchaseMapper.listByPurchaseId(projectPurchase.getPurchaseId());
		if(projectPurchases.size()>0){
			String message="采购单号已存在，请重新填写！";
			log.info("采购单号已存在，请重新填写！");
			throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
		}
		//创建人信息
		OaAdmin adminPerson = oaAdminMapper.selectOaAdminDetailById(oaProjectPurchasePage.getAdminId());
		projectPurchase.setApplyPerson(adminPerson.getName());

		projectPurchase.setId(null);

		//审核人信息
		OaAdmin checkPerson = oaAdminMapper.selectOaAdminDetail(oaProjectPurchasePage.getAuditor());
		//审批表数据赋值
		OaApprove oaApprove=new OaApprove();
		//可能会有多个抄送人
		String copyPerson = oaProjectPurchasePage.getCcPerson();
		String[] copyPersonList = copyPerson.split(",");
		if(copyPersonList.length>1){
			String copyPersonString=null;
			for (int i=0;i<copyPersonList.length;i++){
				OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPersonList[i]);
				if(i>0){
					copyPersonString=copyPersonString+","+ccPerson.getId();
				}else {
					copyPersonString=ccPerson.getId().toString();
				}
			}
			oaApprove.setCopyUids(copyPersonString);
		}else {
			OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPerson);
			oaApprove.setCopyUids(ccPerson.getId().toString());
		}

		//补充项目采购数据
		projectPurchase.setAdminName(checkPerson.getName());
		projectPurchase.setCopyName(copyPerson);
		projectPurchaseMapper.insert(projectPurchase);

		//插入子表数据
		if(null!=projectPurchaseApplyList){
			for (ProjectPurchaseApply p:projectPurchaseApplyList) {
				CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceService.selectCommodityByCommodityCode(p.getCommodityCode());
				p.setId(null);
				p.setCreateTime(null);
				p.setPpId(projectPurchase.getId());
				p.setSpecifications(commodityPriceQueryListVO.getSpecification());
				p.setSupplyPeriod(commodityPriceQueryListVO.getSupplyCycle());
				int num = projectPurchaseApplyMapper.insert(p);
				if(num!=1){
					throw new ServiceException(ServiceCode.ERR_INSERT,"项目采购申请子表插入失败！");
				}
				log.info("项目采购申请表插入成功！");
			}
		}

		//审批表数据赋值
		oaApprove.setType(28);
		oaApprove.setFlowId(28);
		oaApprove.setPurchaseId(oaProjectPurchasePage.getPurchaseId());
		oaApprove.setAdminId(adminPerson.getId());
		oaApprove.setDepartmentId(adminPerson.getDid());
		oaApprove.setCheckStepSort(0);
		oaApprove.setCheckAdminIds(checkPerson.getId().toString());
		oaApprove.setCheckStatus(0);
		oaApprove.setDetailType(0);
		oaApprove.setProjectName(oaProjectPurchasePage.getProjectName());
		Long createTime = System.currentTimeMillis();
		createTime/=1000L;
		oaApprove.setMakeTime(createTime.intValue());
		oaApprove.setCreateTime(createTime.intValue());
		List<OaApprove> oaApproves = oaApproveMapper.listOaProjectApprove();
		OaApprove oa = oaApproves.get(oaApproves.size()-1);
		int num=oa.getId();
		oaApprove.setId(++num);
		oaApprove.setPpId(projectPurchase.getId());
		int insertOaApprove = oaApproveMapper.insert(oaApprove);
		if(insertOaApprove!=1){
			String message="审批表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//审批记录表数据赋值
		OaFlowRecord oaFlowRecord=new OaFlowRecord();
		oaFlowRecord.setActionId(num);
		oaFlowRecord.setStepId(0);
		oaFlowRecord.setCheckUserId(checkPerson.getId());
		oaFlowRecord.setStatus(0);
		oaFlowRecord.setType(1);
		oaFlowRecord.setCheckTime(createTime.intValue());
		oaFlowRecord.setPpId(projectPurchase.getId());
		int insertOaFlowRecord = oaFlowRecordMapper.insert(oaFlowRecord);
		if(insertOaFlowRecord!=1){
			String message="审批记录表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//审批步骤表数据赋值
		OaFlowStep oaFlowStep=new OaFlowStep();
		oaFlowStep.setActionId(num);
		oaFlowStep.setFlowType(0);
		oaFlowStep.setFlowUids(checkPerson.getId());
		oaFlowStep.setSort(0);
		oaFlowStep.setType(1);
		oaFlowStep.setCreateTime(createTime.intValue());
		oaFlowStep.setPpId(projectPurchase.getId());
		int insertOaFlowStep = oaFlowStepMapper.insert(oaFlowStep);
		if(insertOaFlowStep!=1){
			String message="审批步骤表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//消息表数据赋值
		OaMessage oaMessage=new OaMessage();
		oaMessage.setActionId(num);
		oaMessage.setTitle("您收到一个【项目采购申请审批】，请及时审批");
		oaMessage.setContent("您有一个新的【项目采购申请审批】需要处理。");
		oaMessage.setTemplate(60);
		oaMessage.setFromUid(adminPerson.getId());
		oaMessage.setToUid(checkPerson.getId());
		oaMessage.setStatus(1);
		oaMessage.setIsDraft(1);
		oaMessage.setModuleName("project");
		oaMessage.setControllerName("approve");
		oaMessage.setActionName("add");
		oaMessage.setSendTime(createTime.intValue());
		oaMessage.setCreateTime(createTime.intValue());
		oaMessage.setPpId(projectPurchase.getId());
		int insertOaMessage = oaMessageMapper.insert(oaMessage);
		if(insertOaMessage!=1){
			String message="消息表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}
	}


	/**
	 * 根据id修改项目采购isDelete状态
	 * @param id
	 * @return
	 */
	@Override
	public void updateProjectPurchaseIsDeleteById(String id) {
		int num = projectPurchaseMapper.updateProjectPurchaseIsDeleteById(id);
		if(num!=1){
			String message="删除失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_UPDATE,message);
		}
	}


	/**
	 * 根据id修改项目采购申请关联数据
	 * @param updateOaProjectPurchaseDTO
	 */
	@Override
	@Transactional
	public void updateProjectPurchaseById(UpdateOaProjectPurchaseDTO updateOaProjectPurchaseDTO) {
		//删除项目采购关联数据
		projectPurchaseApplyMapper.deleteByPPId(updateOaProjectPurchaseDTO.getId());
		oaApproveMapper.deleteOaApproveByPpId(updateOaProjectPurchaseDTO.getId());
		oaFlowRecordMapper.deleteOaFlowRecordByPpId(updateOaProjectPurchaseDTO.getId());
		oaFlowStepMapper.deleteOaFlowStepByPpId(updateOaProjectPurchaseDTO.getId());
		oaMessageMapper.deleteOaMessageByPcaId(updateOaProjectPurchaseDTO.getId());

		//准备数据重新插入
		//项目采购详情
		List<ProjectPurchaseApply> projectPurchaseApplyList = updateOaProjectPurchaseDTO.getProjectPurchaseApplyList();
		ProjectPurchase projectPurchase=new ProjectPurchase();
		BeanUtils.copyProperties(updateOaProjectPurchaseDTO,projectPurchase);
		//根据项目名称获取项目详情
		OaProject oaProjectDetails = oaProjectMapper.selectOaProjectByName(updateOaProjectPurchaseDTO.getProjectName());
		projectPurchase.setPjId(oaProjectDetails.getProjectId());

		//根据id获取项目阶段详情
		List<OaStep> oaSteps = oaStepMapper.listOaStep(oaProjectDetails.getId());
		for (OaStep oaStep:oaSteps) {
			Long createTime = System.currentTimeMillis();
			Integer currentTime = createTime.intValue();
			Integer startTime = oaStep.getStartTime();
			Integer endTime = oaStep.getEndTime();
			if(currentTime>startTime&&currentTime<endTime){
				//TODO 项目阶段赋值已修改
				projectPurchase.setProjectStage(oaStep.getFlowName());
			}
		}
		BigDecimal purchaseMoney=new BigDecimal(0.00);
		log.info("projectPurchaseApplyList:"+projectPurchaseApplyList.toArray().toString());
		for(ProjectPurchaseApply projectPurchaseApply:projectPurchaseApplyList){
			BigDecimal num = BigDecimal.valueOf(projectPurchaseApply.getRequestedQuantity());
			CommodityPriceQueryListVO commodityPriceQueryListVO = commodityPriceService.selectCommodityByCommodityCode(projectPurchaseApply.getCommodityCode());
			if(null!=commodityPriceQueryListVO) {
				purchaseMoney = commodityPriceQueryListVO.getPurchaseIncludTax().multiply(num);
			}
		}
		projectPurchase.setPurchaseMoney(purchaseMoney);
		//创建人信息
		OaAdmin adminPerson = oaAdminMapper.selectOaAdminDetailById(updateOaProjectPurchaseDTO.getAdminId());
		projectPurchase.setApplyPerson(adminPerson.getName());

		//审核人信息
		OaAdmin checkPerson = oaAdminMapper.selectOaAdminDetail(updateOaProjectPurchaseDTO.getAuditor());
		//可能会有多个抄送人
		String copyPerson = updateOaProjectPurchaseDTO.getCcPerson();

		//补充项目采购数据
		projectPurchase.setCopyName(copyPerson);
		projectPurchase.setAdminName(checkPerson.getName());
		//TODO  修改项目采购信息
		projectPurchaseMapper.updateProjectPurchaseById(projectPurchase);

		//插入子表数据
		if(null!=projectPurchaseApplyList){
			for (ProjectPurchaseApply p:projectPurchaseApplyList) {
				System.out.println("准备插入采购申请表子表");
				p.setId(null);
				p.setPpId(projectPurchase.getId());
				projectPurchaseApplyMapper.insert(p);
				log.info("项目采购申请表插入成功！");
			}
		}

		//时间格式
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = simpleDateFormat.format(new Date());

		//获取日志表数据信息
		/*List<OaAdminLog> oaAdminLogs = oaAdminLogMapper.oaAdminLogList();
		OaAdminLog oAdminLog = oaAdminLogs.get(oaAdminLogs.size() - 1);*/
		Integer oaAdminLogId=oaAdminLogMapper.selectOaAdminLogMaxId()+1;

		//生成项目费用申请修改日志
		OaAdminLog oaAdminLog=new OaAdminLog();
		oaAdminLog.setId(oaAdminLogId);
		oaAdminLog.setUid(adminPerson.getId());
		oaAdminLog.setName(adminPerson.getName());
		oaAdminLog.setType("edit");
		oaAdminLog.setAction("编辑");
		oaAdminLog.setSubject("项目采购");
		oaAdminLog.setTitle("新建/编辑");
		oaAdminLog.setContent(adminPerson.getName()+"在"+time+"编辑了项目采购");
		oaAdminLog.setModule("project");
		oaAdminLog.setController("rule");
		oaAdminLog.setRuleMenu("");
		oaAdminLog.setIp("");
		oaAdminLog.setParamId(1);
		oaAdminLog.setParam("");
		oaAdminLog.setStatus(1);

		//数据创建时间
		Long createTime = System.currentTimeMillis();
		createTime/=1000L;
		oaAdminLog.setCreateTime(createTime.intValue());
		oaAdminLogMapper.insert(oaAdminLog);

		//审批表数据赋值
		OaApprove oaApprove=new OaApprove();

		//抄送人数据赋值
		String[] copyPersonList = copyPerson.split(",");
		if(copyPersonList.length>1){
			String copyPersonString=null;
			for (int i=0;i<copyPersonList.length;i++){
				OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPersonList[i]);
				if(i>0){
					copyPersonString=copyPersonString+","+ccPerson.getId();
				}else {
					copyPersonString=ccPerson.getId().toString();
				}
			}
			oaApprove.setCopyUids(copyPersonString);
		}else {
			OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPerson);
			oaApprove.setCopyUids(ccPerson.getId().toString());
		}


		//审批表数据赋值
		oaApprove.setType(28);
		oaApprove.setFlowId(28);
		oaApprove.setPurchaseId(updateOaProjectPurchaseDTO.getPurchaseId());
		oaApprove.setAdminId(adminPerson.getId());
		oaApprove.setDepartmentId(adminPerson.getDid());
		oaApprove.setCheckStepSort(0);
		oaApprove.setCheckAdminIds(checkPerson.getId().toString());
		oaApprove.setCheckStatus(0);
		oaApprove.setDetailType(0);
		oaApprove.setProjectName(updateOaProjectPurchaseDTO.getProjectName());
		createTime/=1000L;
		oaApprove.setMakeTime(createTime.intValue());
		oaApprove.setCreateTime(createTime.intValue());
		List<OaApprove> oaApproves = oaApproveMapper.listOaProjectApprove();
		OaApprove oa = oaApproves.get(oaApproves.size()-1);
		int num=oa.getId();
		oaApprove.setId(++num);
		oaApprove.setPpId(projectPurchase.getId());
		int insertOaApprove = oaApproveMapper.insert(oaApprove);
		if(insertOaApprove!=1){
			String message="审批表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//审批记录表数据赋值
		OaFlowRecord oaFlowRecord=new OaFlowRecord();
		oaFlowRecord.setActionId(num);
		oaFlowRecord.setStepId(0);
		oaFlowRecord.setCheckUserId(checkPerson.getId());
		oaFlowRecord.setStatus(0);
		oaFlowRecord.setType(1);
		oaFlowRecord.setCheckTime(createTime.intValue());
		oaFlowRecord.setPpId(projectPurchase.getId());
		int insertOaFlowRecord = oaFlowRecordMapper.insert(oaFlowRecord);
		if(insertOaFlowRecord!=1){
			String message="审批记录表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//审批步骤表数据赋值
		OaFlowStep oaFlowStep=new OaFlowStep();
		oaFlowStep.setActionId(num);
		oaFlowStep.setFlowType(0);
		oaFlowStep.setFlowUids(checkPerson.getId());
		oaFlowStep.setSort(0);
		oaFlowStep.setType(1);
		oaFlowStep.setCreateTime(createTime.intValue());
		oaFlowStep.setPpId(projectPurchase.getId());
		int insertOaFlowStep = oaFlowStepMapper.insert(oaFlowStep);
		if(insertOaFlowStep!=1){
			String message="审批步骤表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}

		//消息表数据赋值
		OaMessage oaMessage=new OaMessage();
		oaMessage.setActionId(num);
		oaMessage.setTitle("您收到一个【项目采购申请审批】，请及时审批");
		oaMessage.setContent("您有一个新的【项目采购申请审批】需要处理。");
		oaMessage.setTemplate(60);
		oaMessage.setFromUid(adminPerson.getId());
		oaMessage.setToUid(checkPerson.getId());
		oaMessage.setStatus(1);
		oaMessage.setIsDraft(1);
		oaMessage.setModuleName("project");
		oaMessage.setControllerName("approve");
		oaMessage.setActionName("add");
		oaMessage.setSendTime(createTime.intValue());
		oaMessage.setCreateTime(createTime.intValue());
		oaMessage.setPpId(projectPurchase.getId());
		int insertOaMessage = oaMessageMapper.insert(oaMessage);
		if(insertOaMessage!=1){
			String message="消息表数据插入失败，请联系管理员！";
			throw new ServiceException(ServiceCode.ERR_INSERT,message);
		}
	}
}
